﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Money.Interfaces;
using Money.Data;
using One.Web;
using One.Framework;
using Money.Entities;

public partial class Admin_AccountManagement : System.Web.UI.Page
{
    #region fields

    private IAccountService accountService;
    protected List<Account> accounts;
    private AccountOrderBy orderBy;
    private OrderDirection orderDirection;
    private string redirectUrl = "AccountManagement.aspx";
    private int success;
    private string errorIds;
    private int accountType;
    private int currencyType;
    private int totalRecords;

    #endregion

    #region events

    protected void Page_Load(object sender, EventArgs e)
    {
        accountService = ServiceFactory.Create<IAccountService>();

        GetParams();
        LoadData();
        UpdateView();
    }

    protected void DeleteAccounts(object sender, EventArgs e)
    {
        
    }

    #endregion

    #region private functions

    private void GetParams()
    {
        orderBy = QueryStringUtility.GetEnum<AccountOrderBy>("OrderBy", AccountOrderBy.AccountId.ToString());
        orderDirection = QueryStringUtility.GetEnum<OrderDirection>("OrderDirection", OrderDirection.ASC.ToString());
        success = QueryStringUtility.GetInt32("success", 0);
        errorIds = QueryStringUtility.GetString("idserror", "");
        accountType = QueryStringUtility.GetInt32("accountType", 0);
        currencyType = QueryStringUtility.GetInt32("currencyType", 0);
    }

    private void LoadData()
    {
        accounts = accountService.GetAccounts(orderBy.ToString(), orderDirection.ToString(), pager.PageIndex,
                                        MoneyManagerConfig.AdminPageSize, out totalRecords);
    }

    private void UpdateView()
    {
        repeaterAccounts.DataSource = accounts;
        repeaterAccounts.DataBind();

        (Master as Admin_MasterPage).SelectedTab = "accountManagement";
        pageTitle.Text = Resources.Strings.AccountManagement;

        messageLabel.Visible = success != 0;

        switch (success)
        {
            case 1:
                messageLabel.Text = Resources.Strings.AddAccountSuccess;
                break;
            case 2:
                messageLabel.Text = Resources.Strings.UpdateAccountSuccess;
                break;
            case 3:
                messageLabel.Text = Resources.Strings.DeleteAccountSuccess;
                break;
            case -1:
                messageLabel.Text = Resources.Strings.AddAccountUnSuccess;
                break;
            case -2:
                messageLabel.Text = Resources.Strings.UpdateAccountUnSuccess;
                break;
            case -3:
                messageLabel.Text = string.Format(Resources.Strings.DeleteAccountUnSuccess, errorIds);
                break;

        }
    }

    #endregion

    #region public functions

    public string GetOrderByLink(string propertyName)
    {
        OrderDirection newOrderDirection = orderDirection;
        string url = CreateUrlParams();
        if (propertyName == orderBy.ToString())
        {
            newOrderDirection = orderDirection == OrderDirection.ASC ? OrderDirection.DESC : OrderDirection.ASC;
        }
        return String.Format("{0}{4}OrderBy={1}&OrderDirection={2}&Page={3}", url,
            propertyName, newOrderDirection.ToString(), pager.PageIndex, url.IndexOf('?') > 0 ? "&" : "?");
    }

    private string CreateUrlParams()
    {
        return redirectUrl;
    }

    public string GetOrderDirectionIndicator(string propertyName)
    {
        if (propertyName == orderBy.ToString())
        {
            return String.Format("<img src=\"../images/{0}.gif\" alt=\"{0}\" align=\"absmiddle\" />", orderDirection);
        }
        return "";
    }

    #endregion
}
